/**
 * Created by merlin on 07/05/2018.
 */
var ERROR_CODE={
    SUCCESS:0,
    FAILURE:-1

}

var BODY_TYPE_CONSTANT ={
    FORM_ENCODE: "FORM_ENCODE",
    JSON_STRING: "JSON_STRING"
};

var METHOD_TYPE = {
    POST: "POST",
    GET: "GET"
};

var isMock = function(uri){
    if(MOCK[uri] && MOCK[uri]['mock']===true){
        return true;
    }
    return false;
};
var RequestClient = {
    create : function (uri, headers, data, fn) {
        var obj = {
            props : {
                type : METHOD_TYPE.GET,
                contentType : "application/json",
                dataType : "json",
                async : true,
                bodyType: BODY_TYPE_CONSTANT.FORM_ENCODE
            },
            genUrl : function(uri){
                if(host.indexOf("test") > -1) {
                    return "http://test.huakai.me/api2" + uri;
                }
                if(host.indexOf("local") > -1) {
                    return "/api2" + uri;
                }
                return "/api" + uri;
            },
            createData: function () {
                if (this.props.bodyType === BODY_TYPE_CONSTANT.JSON_STRING) {
                    return JSON.stringify(data);
                }
                return data;
            },
            doRequest : function () {
                if(isMock(uri)){
                    return fn(MOCK[uri]['res']);
                }
                $.ajax({
                    url : this.genUrl(uri),
                    type : this.props.type,
                    contentType : this.props.contentType,
                    dataType : this.props.dataType,
                    async : this.props.async,
                    headers : headers,
                    data : this.createData(),
                    success : function (resp, uri) {
                        fn(resp);
                    },
                    error : function (resp) {
                       //alertMsg(resp.responseJSON.msg);
                    }
                });
            },
            setType : function(type) {
                if (type === METHOD_TYPE.POST) {
                    this.setBodyType(BODY_TYPE_CONSTANT.JSON_STRING);
                }
                this.props.type = type;
                return this;
            },
            setContentType : function (contentType) {
                this.props.contentType = contentType;
                return this;
            },
            setDataType : function (dataType) {
                this.props.dataType = dataType;
                return this;
            },
            setAsync : function (async) {
                this.props.async = async;
                return this;
            },
            setBodyType: function (bodyType) {
                this.props.bodyType = bodyType;
                return this;
            }
        };
        return obj;
    }
}

var alertMsg = function (msg) {
    alert(msg); //todo 修改样式
}

var MOCK = {
    //故事王
    "/h5/activity/voice/story/ranking" : {
        "mock" : false,
        "res" :
            {
                "data":
                    {
                        "popularityRankingList" : [
                            {
                                "avatarUrl": "img/headpic.jpg",
                                "customerId": 0,
                                "nickName": "张三"
                            },
                            {
                                "avatarUrl": "img/headpic.jpg",
                                "customerId": 0,
                                "nickName": "李四"
                            },
                            {
                                "avatarUrl": "img/headpic.jpg",
                                "customerId": 0,
                                "nickName": "王五"
                            }
                        ],
                        "storyRankingList": [
                            {
                                "avatarUrl": "img/headpic.jpg",
                                "customerId": 0,
                                "nickName": "张龙"
                            },
                            {
                                "avatarUrl": "img/headpic.jpg",
                                "customerId": 0,
                                "nickName": "赵虎"
                            },
                            {
                                "avatarUrl": "img/headpic.jpg",
                                "customerId": 0,
                                "nickName": "王朝"
                            }
                        ]
                    }
                ,
                "code": 0,
                "message": ""
            }
    }
};